package com.shanghai.shanghaielectric.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shanghai.shanghaielectric.R;
import com.shanghai.shanghaielectric.controller.ParameterSetController.PassiveComponentType;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2018/1/10.
 */

public class CapacitanceStateView extends LinearLayout {

    private LinearLayout mContainerLL;
    private LinearLayout tv_capacitor_state_tv_nc_ll;

    private List<ImageView> mIvList;
    private List<Integer> mEntityList;

    private LinearLayout mTvContainerLL;
    private List<String> mTvEntityList;

    private PassiveComponentType mType;

    public CapacitanceStateView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public CapacitanceStateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CapacitanceStateView(Context context) {
        super(context);
        init(context);
    }

    //    初始化只画出了白线和斜杠，动态的电容呢？
    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.capacitor_state_view, this);//画白线和斜杠
        mContainerLL = (LinearLayout) view.findViewById(R.id.capacitor_state_ll);//画白线
        tv_capacitor_state_tv_nc_ll = (LinearLayout) view.findViewById(R.id.capacitor_name_tv_nc_ll);//白线下的电容名称

        mIvList = new ArrayList<ImageView>();
        mEntityList = new ArrayList<Integer>();

        mTvContainerLL = (LinearLayout) view.findViewById(R.id.capacitor_state_tv_ll);//白线上的电容数值
        mTvEntityList = new ArrayList<String>();//设置电容上的数字实体
    }

    private void setData(List<Integer> list) {
        mEntityList.clear();
        mEntityList.addAll(list);
        mContainerLL.removeAllViews();
        mIvList.clear();
        for (int i = 0; i < mEntityList.size(); i++) {
            addActionImage(mEntityList.get(i));
        }
    }

    public void refreshData(List<Integer> list) {
        if (list == null) {
            return;
        }
        if (list.size() == mEntityList.size()) {
            for (int i = 0; i < list.size(); i++) {
                mIvList.get(i).setBackgroundResource(list.get(i));
            }
        } else {
            setData(list);
        }
    }

    //刷新电容的数值
    public void refreshTvData(int passiveComponentType) {
        if (mType == null || mType != PassiveComponentType.getEnum(passiveComponentType)) {
            mType = PassiveComponentType.getEnum(passiveComponentType);
        } else {
            return;
        }
        mTvEntityList.clear();
        mTvContainerLL.removeAllViews();
        tv_capacitor_state_tv_nc_ll.removeAllViews();
        mTvEntityList = new ArrayList<String>();
        if (mType == PassiveComponentType.KVAR_240) {
            mTvEntityList.add("50kVar");
            mTvEntityList.add("50kVar");
            mTvEntityList.add("50kVar");
            mTvEntityList.add("40kVar");
            mTvEntityList.add("20kVar");
        } else if (mType == PassiveComponentType.KVAR_300) {
            mTvEntityList.add("60kVar");
            mTvEntityList.add("60kVar");
            mTvEntityList.add("60kVar");
            mTvEntityList.add("40kVar");
            mTvEntityList.add("30kVar");
            mTvEntityList.add("20kVar");
        } else if (mType == PassiveComponentType.KVAR_360) {
            mTvEntityList.add("60kVar");
            mTvEntityList.add("60kVar");
            mTvEntityList.add("60kVar");
            mTvEntityList.add("60kVar");
            mTvEntityList.add("40kVar");
            mTvEntityList.add("30kVar");
            mTvEntityList.add("20kVar");
        }
        setTv(mTvEntityList);
    }

    //设置了电容上的数字 kvar
    private void setTv(List<String> list) {
        for (int i = 0; i < mTvEntityList.size(); i++) {
            addActionTv(mTvEntityList.get(i), i);
        }
    }

    //横线上方的电容的数值 kvar
    private TextView addActionTv(String text, int index) {
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(85, LayoutParams.WRAP_CONTENT);
        param.setMargins(3, 0, 2, 0);//新添加@chb
        param.gravity = Gravity.CENTER | Gravity.RIGHT;
        TextView tv = new TextView(getContext());
        tv.setGravity(Gravity.CENTER);
//        tv.setTextSize(getResources().getDimension(R.dimen.sp28));//适配SVG屏的大小  绿屏
        tv.setTextSize(getResources().getDimension(R.dimen.sp20));//适配APF屏的大小    蓝屏from杭州
        tv.setTextColor(getResources().getColor(R.color.black));
        tv.setText(text);

        //-----chb横线下方电容的序号值   c
        TextView tv_nc = new TextView(getContext());
        tv_nc.setGravity(Gravity.CENTER);
//        tv_nc.setTextSize(getResources().getDimension(R.dimen.sp28));//适配SVG屏幕的大小
        tv_nc.setTextSize(getResources().getDimension(R.dimen.sp16));//适配APF屏幕的大小
        tv_nc.setTextColor(getResources().getColor(R.color.red));
        tv_nc.setText(index + 1 + "C");

        mTvContainerLL.addView(tv, param);
        tv_capacitor_state_tv_nc_ll.addView(tv_nc, param);
        return tv;
    }

    //将电容图片添加进去
    private ImageView addActionImage(int drawbleId) {
        ImageView img = createActionImage(drawbleId);
        mContainerLL.addView(img, createParams());
        mIvList.add(img);
        return img;
    }

    //获取已经添加的图片，设置ImageView的属性，返回图片，绘制成功
    private ImageView createActionImage(int drawbleId) {
        ImageView img = new ImageView(getContext());
        img.setBackgroundResource(drawbleId);
        img.setLayoutParams(createParams());//设置ImageView的属性
        return img;
    }

    private LinearLayout.LayoutParams createParams() {
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
//        param.setMargins(24, 0, 24, 14);//(10,0,10,0)@chb   for SVG绿色屏
        param.setMargins(14, 0, 14, 10);//(10,0,10,0)@chb   for APF蓝色屏
        param.gravity = Gravity.CENTER | Gravity.RIGHT;
        return param;
    }

}

